package com.jichaoyun.sys.controller;

import com.jichaoyun.common.model.PagedResult;
import com.jichaoyun.common.model.Result;
import com.jichaoyun.sys.service.IFlowHourService;
import com.jichaoyun.sys.service.IRealdataCacheService;
import com.jichaoyun.sys.service.IStationInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = {"查询数据接口"})
@RequestMapping("/sys/DataQuery")
public class DataQueryController {

    @Autowired
    private IStationInfoService stationInfoService;

    @Autowired
    private IFlowHourService flowHourService;

    @Autowired
    private IRealdataCacheService realdataCacheService;




    @ApiOperation("查询数据，历史数据，查询某站点选定日期和时分秒的数据")
    @GetMapping("/getHourDataByTime")
    public Result<Map<String,Object>> getHourDataByTime(String stationName,String date) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime datetime = LocalDateTime.parse(date,dateTimeFormatter);

        return Result.success(flowHourService.getDataByTime(stationName,datetime));
    }

    @ApiOperation("查询数据，流量分析，分析选定日期的单个站点的流速数据")
    @GetMapping("/analyzeFlowByDate")
    public Result<Map<String,Object>> analyzeFlowByDate(String stationName,String date) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate datetime = LocalDate.parse(date,dateTimeFormatter);

        return Result.success(flowHourService.getFlowByDateTime(stationName,datetime));
    }

    @ApiOperation("查询数据，温度分析，分析选定日期的单个站点的温度数据")
    @GetMapping("/analyzeTempByDate")
    public Result<Map<String,Object>> analyzeTempByDate(String stationName,String date) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate datetime = LocalDate.parse(date,dateTimeFormatter);

        return Result.success(flowHourService.getTempByDateTime(stationName,datetime));
    }

    @ApiOperation("查询数据，压力分析，分析选定日期的单个站点的压力数据")
    @GetMapping("/analyzePresByDate")
    public Result<Map<String,Object>> analyzePresByDate(String stationName,String date) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate datetime = LocalDate.parse(date,dateTimeFormatter);

        return Result.success(flowHourService.getPresByDateTime(stationName,datetime));
    }

    @ApiOperation("查询数据，流量分析，分析所有站点选定日期的流速数据")
    @GetMapping("/analyzeAllFlowByDate")
    public Result<PagedResult> analyzeAllFlowByDate(String date, Integer pageNo) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate datetime = LocalDate.parse(date,dateTimeFormatter);

        return Result.success(flowHourService.getAllFlowByDate(datetime,pageNo,8));
    }

    @ApiOperation(value = "查询数据，流量分析，流量分析表，导出到Excel")
    @GetMapping("/DownloadAnalyzeFlow")
    public void DownloadAnalyzeFlow(String localDate, HttpServletResponse response) throws IOException {

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate datetime = LocalDate.parse(localDate,dateTimeFormatter);

        flowHourService.getAllFlowAnalyzeExcel(datetime,response);
    }

    @ApiOperation("查询数据，温度分析，分析所有站点选定日期的温度数据")
    @GetMapping("/analyzeAllTempByDate")
    public Result<PagedResult> analyzeAllTempByDate(String date, Integer pageNo) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate datetime = LocalDate.parse(date,dateTimeFormatter);

        return Result.success(flowHourService.getAllTempByDate(datetime,pageNo,8));
    }

    @ApiOperation(value = "查询数据，温度分析，温度分析表，导出到Excel")
    @GetMapping("/DownloadAnalyzeTemp")
    public void DownloadAnalyzeTemp(String localDate, HttpServletResponse response) throws IOException {

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate datetime = LocalDate.parse(localDate,dateTimeFormatter);

        flowHourService.getAllTempAnalyzeExcel(datetime,response);
    }

    @ApiOperation("查询数据，压力分析，分析所有站点选定日期的压力数据")
    @GetMapping("/analyzeAllPresByDate")
    public Result<PagedResult> analyzeAllPresByDate(String date, Integer pageNo) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate datetime = LocalDate.parse(date,dateTimeFormatter);

        return Result.success(flowHourService.getAllPresByDate(datetime,pageNo,8));
    }

    @ApiOperation(value = "查询数据，压力分析，压力分析表，导出到Excel")
    @GetMapping("/DownloadAnalyzePres")
    public void DownloadAnalyzePres(String localDate, HttpServletResponse response) throws IOException {

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate datetime = LocalDate.parse(localDate,dateTimeFormatter);

        flowHourService.getAllPresAnalyzeExcel(datetime,response);
    }

    @ApiOperation("查询数据，实时曲线，任意区间")
    @GetMapping("/getAnyPeriodRealData")
    public Result<Map<String,List<Object>>> getAnyPeriodRealData(String stationName) {


        return Result.success(realdataCacheService.getHalfHourRealData(stationName));
    }

    @ApiOperation("查询数据，当日流量排名，查询选定日期和时间的当日流量排名")
    @GetMapping("/getFlowRankListByDate")
    public Result<Map<String,Float>> getFlowRankListByDate(String date) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate datetime = LocalDate.parse(date,dateTimeFormatter);

        return Result.success(stationInfoService.getFlowRankListByDate(datetime));
    }

    @ApiOperation("查询数据，当日温度排名，查询选定日期和时间的当日温度排名")
    @GetMapping("/getTempRankListByDate")
    public Result<Map<String,Float>> getTempRankListByDate(String date) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate datetime = LocalDate.parse(date,dateTimeFormatter);

        return Result.success(stationInfoService.getTempRankListByDate(datetime));
    }

    @ApiOperation("查询数据，当日压力排名，查询选定日期和时间的当日压力排名")
    @GetMapping("/getPresRankListByDate")
    public Result<Map<String,Float>> getPresRankListByDate(String date) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate datetime = LocalDate.parse(date,dateTimeFormatter);

        return Result.success(stationInfoService.getPresRankListByDate(datetime));
    }
}
